const Koa = require('koa')//引入koa
const bodyparser = require('koa-bodyparser')//解析post请求
const app = new Koa()//new koa实例对象
const mongoose = require('mongoose')//引入mongodb数据库
const config = require('./config.js')//密码加密配置文件
const cors = require('@koa/cors');//解决跨域
const websocketcallback = require('./controllers/websocket/index')
app.use(cors());//利用cors解决跨域



mongoose.connect(config.db, { useNewUrlParser: true }, (err) => {
    if (err) {
        console.error(err);
    } else {
        console.log('数据库连接成功');
    }
})
app.use(bodyparser())//合并代码解决post请求解析
const user_router = require('./routes/user/index.js')//引入写好的router
app.use(user_router.routes()).use(user_router.allowedMethods())

const httpserver = app.listen(config.port, () => {
    console.log('项目已启动');
})
const {Server} = require('socket.io')
const io = new Server(httpserver,{cors: true});

    io.on("connection", websocketcallback.connection);